package com.ruoyi.web.controller.zsix;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsix.service.IFilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Map;


/**
 * 通用请求处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/fileCommon")
public class FileCommonController extends BaseController
{
    @Autowired
    private IFilService iFilService;


    /**
     * 文件上传SFTP
     *
     * @return
     * @throws Exception
     * @throws Exception
     */
    @Log(title = "附件上传" , businessType = BusinessType.EXPORT)
    @PostMapping("/uploadSftp")
    public AjaxResult uploadFileSftp(@RequestParam("file") MultipartFile file) throws Exception {
        // 获取源文件
        boolean tag = true;
        // 获取文件流/
        InputStream ins = file.getInputStream();
        // 获取文件名称 (包括后缀)
        String fileNameOld = file.getOriginalFilename();
        // 调用上传
        Map<String, Object> result = iFilService.uploadFile(fileNameOld, ins, tag);
       return  success(result);
    }
    @Log(title = "附件下载" , businessType = BusinessType.EXPORT)
    @PostMapping("/downFilesftp")
    public void downFilesftp(@RequestBody JSONObject param, HttpServletResponse response){
        String fileName=param.getString("fileName");
        String filePath=param.getString("filePath");
        iFilService.download(filePath,fileName,response);
    }
}
